
import axios from 'axios'
// 定义shopcarslice
import { createSlice } from '@reduxjs/toolkit';
//02:第二步: 创建购物车切片
const userSlice = createSlice({
    name: 'user', // 相当于vuex 中命名空间, 给切片命名,
    initialState: { // 定义当前切片的初始数据的
        userinfo: {
            name: '钟汉离',
            age: 50
        },
        schoollist: [] // 定义学校列表,
    },
    reducers: {
        addAge(state, actions) {
            state.userinfo.age += actions.payload
        },
        setschoollist(state, actions) {
            state.schoollist = actions.payload
        }
    }
})

// 导出同步的reducers
export const { addAge, setschoollist } = userSlice.actions;


// 定义异步操作state数据的reducers方法
export const setschoollistAsync = (payload) => {
    return (dispatch) => {
        axios.get('https://api.i-lynn.cn/college').then(res => {
            // 调用同步reducers
            dispatch(setschoollist(res.data.data.list1))
        })

    }
}


export default userSlice